import { Application } from 'egg';
import {
    indexRule,
    detailRule,
    readyRule,
    sendRule,
    baseRule,
    payRule,
    orderPayRule,
    refundRule,
    submitRule,
    numberRule,
    submitDetailRule
} from '../validate/order';

/**
 * 用户组管理的路由
 */
export default (app: Application) => {
    const { auth, header } = app.middleware;
    const {
        jwt
    } = app;

    return {
        'order.list': {
            method: 'GET',
            path: '/api/order/list',
            options: {
                tags: ['order/all'],
                description: '订单列表',
                handler: app.controller.order.list,
                middlewares: [jwt, auth('order.list')],
                validate: indexRule,
            },
        },
        'order.detail': {
            method: 'GET',
            path: '/api/order/detail/:id',
            options: {
                tags: ['order/detail'],
                description: '订单详情',
                handler: app.controller.order.orderDetail,
                middlewares: [jwt, auth('order.detail')],
                validate: detailRule,
            },
        },
        'order.ready': {
            method: 'POST',
            path: '/api/order/ready',
            options: {
                tags: ['order/ready'],
                description: '订单配货',
                handler: app.controller.order.ready,
                // middlewares: [jwt, auth('order.ready')],
                validate: readyRule,
            },
        },
        'order.send': {
            method: 'POST',
            path: '/api/order/send',
            options: {
                tags: ['order/send'],
                description: '订单配货',
                handler: app.controller.order.send,
                // middlewares: [jwt, auth('order.send')],
                validate: sendRule,
            },
        },
        'order.finish': {
            method: 'POST',
            path: '/api/order/finish',
            options: {
                tags: ['order/finish'],
                description: '订单完成',
                handler: app.controller.order.finish,
                middlewares: [jwt, auth('order.finish')],
                validate: baseRule,
            },
        },
        // 'order.pay': {
        //     method: 'POST',
        //     path: '/wx/order/pay',
        //     options: {
        //         tags: ['order/pay'],
        //         description: '小程序订单支付',
        //         handler: app.controller.order.pay,
        //         // middlewares: [jwt, auth('order.pay')],
        //         validate: payRule,
        //     },
        // },
        // 'order.id.pay': {
        //     method: 'POST',
        //     path: '/wx/order/id/pay',
        //     options: {
        //         tags: ['order/id/pay'],
        //         description: '小程序订单号支付',
        //         handler: app.controller.order.orderPay,
        //         // middlewares: [jwt, auth('order.id.pay')],
        //         validate: orderPayRule,
        //     },
        // },
        'order.pay.callback': {
            method: 'POST',
            path: '/wx/order/pay/callback',
            options: {
                tags: ['order/id/pay'],
                description: '小程序支付回调',
                handler: app.controller.order.payCallback,
            },
        },
        'order.pay.refund': {
            method: 'POST',
            path: '/api/order/refund',
            options: {
                tags: ['order/pay/refund'],
                description: '小程序订单退款',
                handler: app.controller.order.refund,
                // middlewares: [jwt, auth('order.pay.refund')],
                validate: refundRule,
            },
        },
        'order.cancel': {
            method: 'POST',
            path: '/api/order/cancel',
            options: {
                tags: ['order/cancel'],
                description: '订单取消',
                handler: app.controller.order.cancel,
                // middlewares: [jwt, auth('order.cancel')],
                validate: baseRule,
            },
        },
        'order.submit': {
            method: 'POST',
            path: '/wx/order/submit',
            options: {
                tags: ['order/submit'],
                description: '小程序订单提交',
                handler: app.controller.order.submit,
                // middlewares: [jwt, auth('order.submit')],
                validate: submitRule,
            },
        },
        'order.submit.detail': {
            method: 'GET',
            path: '/wx/order/submit/detail',
            options: {
                tags: ['order/submit/detail'],
                description: '小程序订单提交详情',
                handler: app.controller.order.submitDetail,
                // middlewares: [jwt, auth('order.submitDetailRule')],
                validate: submitDetailRule,
            },
        },
        'xxm.order.pay': {
            method: 'POST',
            path: '/xxm/order/pay',
            options: {
                tags: ['order/pay'],
                description: '小程序订单支付-餐饮',
                handler: app.controller.wx.order.pay,
                middlewares: [jwt, header()],
                validate: payRule,
            },
        },
        'xxm.order.print': {
            method: 'POST',
            path: '/xxm/order/print',
            options: {
                tags: ['order/pay'],
                description: '小程序订单打印-餐饮',
                handler: app.controller.order.printMSg,
                middlewares: [header()],
                validate: orderPayRule,
            },
        },
        'xxm.order.list': {
            method: 'GET',
            path: '/xxm/order/list',
            options: {
                tags: ['order/all'],
                description: '订单列表',
                handler: app.controller.wx.order.list,
                middlewares: [jwt, header()],
                validate: indexRule,
            },
        },
        'xxm.order.number': {
            method: 'POST',
            path: '/xxm/order/number',
            options: {
                tags: ['order/all'],
                description: '订单数量',
                handler: app.controller.wx.order.number,
                middlewares: [jwt, header()],
                validate: numberRule,
            },
        },
        'xxm.order.detail': {
            method: 'GET',
            path: '/xxm/order/detail/:id',
            options: {
                tags: ['order/detail'],
                description: '订单详情',
                handler: app.controller.wx.order.detail,
                middlewares: [jwt, header()],
                validate: detailRule,
            },
        },

        'xxm.order.payById': {
            method: 'POST',
            path: '/xxm/order/pay/id',
            options: {
                tags: ['order/id/pay'],
                description: '小程序订单号支付',
                handler: app.controller.wx.order.orderPay,
                middlewares: [jwt, header()],
                validate: orderPayRule,
            },
        },
    };
};
